<template>
  <Form ref="target" :validation-schema="ruler" v-slot="{ errors }">
    <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-user"></i>
          <Field
            class="input"
            type="text"
            v-model="formData.account"
            :class="{ err: errors.account }"
            name="account"
            placeholder="请输入用户名"
          />
        </div>
        <div class="error" v-if="errors.account">
          <i class="iconfont icon-warning" />{{ errors.account }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            class="input"
            type="text"
            v-model="formData.mobile"
            :class="{ err: errors.mobile }"
            name="mobile"
            placeholder="请输入手机号"
          />
        </div>
        <div class="error" v-if="errors.mobile">
          <i class="iconfont icon-warning" />{{ errors.mobile }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            class="input"
            type="text"
            :class="{ err: errors.code }"
            v-model="formData.code"
            name="code"
            placeholder="请输入验证码"
          />
          <span class="code" @click="send">{{
            time === 0 ? "发送验证码" : `${time}秒后重发`
          }}</span>
        </div>
        <div class="error" v-if="errors.code">
          <i class="iconfont icon-warning" />{{ errors.code }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            class="input"
            type="password"
            :class="{ err: errors.password }"
            v-model="formData.password"
            name="password"
            placeholder="请输入密码"
          />
        </div>
        <div class="error" v-if="errors.password">
          <i class="iconfont icon-warning" />{{ errors.password }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            class="input"
            type="password"
            :class="{ err: errors.rePassword }"
            v-model="formData.rePassword"
            name="rePassword"
            placeholder="请确认密码"
          />
        </div>
        <div class="error" v-if="errors.rePassword">
          <i class="iconfont icon-warning" />{{ errors.rePassword }}
        </div>
      </div>
      <a href="javascript:;" class="submit" @click="submit">立即提交</a>
    </div>
  </Form>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
// 引入表单校验功能
import { Form, Field } from 'vee-validate'
// 引入表单校验
import validateForm from '@/utils/validate'
import Message from '@/components/Message.js'
// 引入封装倒计时函数
import { useCountDown } from '@/compositions/index'
import { userQQPatchCode } from '@/api/user'
export default {
  name: 'CallbackPatch',
  components: {
    Form, Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const router = useRouter()
    // 表单校验，
    // 1.定义数据项
    const formData = reactive({
      account: '',
      password: '',
      rePassword: '',
      mobile: '',
      code: ''
    })
    // 2.定义校验规则
    const ruler = {
      account: validateForm.account,
      password: validateForm.password,
      rePassword: validateForm.rePassword,
      mobile: validateForm.mobile,
      code: validateForm.code
    }

    // 兜底校验
    const target = ref(null)
    const submit = () => {
      target.value.validate().then(async (res) => {
        if (res) {
          try {
            await store.dispatch('user/userQQPatchLogin', {
              unionId: props.unionId,
              mobile: formData.mobile,
              account: formData.account,
              password: formData.password,
              code: formData.code
            })
            // 同步购物车
            await store.dispatch('cart/mergeLocalCart')
            Message({ type: 'success', text: '完善信息成功' })
            router.push('/')
          } catch (err) {
            Message({ type: 'error', text: err.response.data.message })
          }
        }
      })
    }

    // 发送手机验证码
    const { time, start } = useCountDown()
    const send = () => {
      // 使用倒计时函数
      // 发送验证码
      if (!validateForm.mobile(formData.mobile)) {
        Message({ type: 'error', text: '手机号格式不正确' })
        return
      }
      // 发请求获取验证码
      if (time.value !== 0) {
        return
      }
      userQQPatchCode(formData.mobile).then(() => {
        start(60)
        Message({ type: 'success', text: '验证码已发送' })
      }).catch(err => {
        console.log(err)
        Message({ type: 'error', text: err.response.data.message })
      })
    }

    return { formData, ruler, target, submit, send, time }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
